<template>
  <div class="header">
    <van-cell-group inset>
      <van-field label="账户" :model-value=addr readonly />
      <van-field label="余额" :model-value=balance readonly />
      <van-field label="权重" :model-value=weight readonly />
      <van-field label="是否投票" :model-value=str readonly />
    </van-cell-group>

  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import useWeb3 from '../util.js';

const { ballotSm, web3 } = useWeb3();

const addr = ref('');
const balance = ref(0);
const weight = ref(0);
const str = ref('');

// 获取当前账户
const accounts = await web3.eth.getAccounts();
addr.value = accounts[0];

// 获取账户余额
web3.eth.getBalance(addr.value).then((ret) => {
  balance.value = web3.utils.fromWei(ret, "ether") + " eth";
});

// 获取个人信息
const getInfo = async () => {
  const act = await ballotSm.methods.getInfo(accounts[0]).call();
  weight.value = act.weight;
  str.value = act.voted ? "已投票" : "未投票";
}


export default {
  name: 'InfoView',
  props: {

  },
  setup() {
    onMounted(async () => {
      console.log(`the component is now mounted.`);
      await getInfo();
    })
    return {
      addr,
      balance,
      weight,
      str
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.header {
  margin-top: 40px;
}
</style>